	//
//  KDNewsCell.m
//  YuQingXiaoFang
//
//  Created by wangdongdong on 18/6/8.
//  Copyright © 2018年 KDTech. All rights reserved.
//

#import "KDNewsCell.h"
#import "KDNewsCellModel.h"
#import "KDNewsCellModelFrame.h"
#import "UIImageView+WebCache.h"
#import "KDNewsRequestTool.h"

@interface KDNewsCell()



@property (weak, nonatomic)  UIImageView *leftView;
@property (weak, nonatomic)  UILabel *titleLb;
@property (weak, nonatomic)  UILabel *timeLb;
@property (weak, nonatomic)  UILabel *sourceLb;
@property (weak, nonatomic)  UILabel *contentLb;
@property (weak, nonatomic)  UIImageView *star1View;
@property (weak, nonatomic)  UIImageView *star2View;
@property (weak, nonatomic)  UIImageView *star3View;
@property (weak, nonatomic)  UIButton *lookBtn;
@property (weak, nonatomic)  UIButton *focusBtn;

@property (nonatomic, retain) NSDateFormatter *fmt;
@end

@implementation KDNewsCell

- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    }
    return _fmt;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *cellId =@"KDNewsCell";
    
    
    KDNewsCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    
    if(cell==nil){
        cell=[[KDNewsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.backgroundColor = BackGroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    cell.similarBtn.layer.cornerRadius = 3.0f;
    //    cell.similarBtn.clipsToBounds = YES;
    //    cell.similarBtn.layer.borderWidth = 0.5;
    //    [cell.reportBtn setImage:[UIImage imageNamed:@"已上报"] forState:UIControlStateDisabled];
    
    
    return cell;
}

-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self setupSubviews];
    return self;
}

-(void) setupSubviews{
    UIImageView *imageView= [[UIImageView alloc] init];
    [self.contentView addSubview:imageView];
    _leftView=imageView;
    
    UILabel *titleLb=[[UILabel alloc] init];
    [self.contentView addSubview:titleLb];
    _titleLb=titleLb;
    
    UILabel *timeLb=[[UILabel alloc] init];
    [self.contentView addSubview:timeLb];
    _timeLb=timeLb;
    
    UILabel *sourceLb=[[UILabel alloc] init];
    [self.contentView addSubview:sourceLb];
    _sourceLb=sourceLb;
    
    UILabel *contentLb=[[UILabel alloc] init];
    contentLb.numberOfLines=2;
    [self.contentView addSubview:contentLb];
    _contentLb=contentLb;
    
    UIButton *focusBtn=[[UIButton alloc] init];
    [focusBtn addTarget:self action:@selector(focusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:focusBtn];
    _focusBtn=focusBtn;
    
    UIButton *lookBtn=[[UIButton alloc] init];
    [focusBtn addTarget:self action:@selector(lookBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:lookBtn];
    _lookBtn=lookBtn;
    
    
    UIImageView *star1View= [[UIImageView alloc] init];
    [self.contentView addSubview:star1View];
    _star1View=star1View;

    UIImageView *star2View= [[UIImageView alloc] init];
    [self.contentView addSubview:star2View];
    _star2View=star2View;
    
    UIImageView *star3View= [[UIImageView alloc] init];
    [self.contentView addSubview:star3View];
    _star3View=star3View;


}


/**
 *  传递模型数据
 */
- (void)setModelFrame:(KDNewsCellModelFrame *) modelFrame
{
    _modelFrame = modelFrame;
    
    // 1.原创微博
    [self setupModelData];
    
    }

- (void)setupModelData{
    KDNewsCellModel *model=self.modelFrame.model;
    
    //图片
    if(model.newsType==NEW_TYPE_INTERNEWS || model.newsType==NEW_TYPE_ABROAD){
        self.leftView.hidden=NO;
        NSString *imageUrl=[KDNewsRequestTool getNewsImageUrlWithParam:model.img_url];
        [self.leftView sd_setImageWithURL: [NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"短信1"]];
        self.leftView.frame = self.modelFrame.leftViewF;
    }else{
        self.leftView.hidden=YES;
    }
    //标题
    self.titleLb.text=model.title;
    self.titleLb.font=KDNewsCellModelTitleFont;
    self.titleLb.frame=self.modelFrame.titleLabelF;

    //日期
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.pushTimes/ 1000.0];
    self.timeLb.text = [self.fmt stringFromDate:date];
    self.timeLb.font=KDNewsCellModelTimeFont;
    self.timeLb.frame=self.modelFrame.timeLabelF;
    
    //来源或作者
    if(model.newsType==NEW_TYPE_INTERNEWS || model.newsType==NEW_TYPE_ABROAD){
        self.sourceLb.text = model.author;
    }else{
        self.sourceLb.text = model.source;
    }
    self.sourceLb.font=KDNewsCellModelTimeFont;
    self.sourceLb.frame=self.modelFrame.sourceLabelF;
    
    //内容
    if(model.newsType!=NEW_TYPE_INTERNEWS && model.newsType!=NEW_TYPE_ABROAD){
        self.contentLb.hidden=NO;
        self.contentLb.text = model.summary;
        self.contentLb.font=KDNewsCellModelContentFont;
        self.contentLb.frame=self.modelFrame.contentLabelF;
        [self.contentLb sizeToFit];
        
    
    }else{
        self.contentLb.hidden=YES;
    }
    //预警级别
    if(model.newsLevel==0 || model.newsLevel==1 || model.newsLevel==2 || model.newsLevel==3){
        self.star1View.image=[UIImage imageNamed:@"收藏选中"];
        self.star1View.frame=self.modelFrame.star1ViewF;
    }
    if(model.newsLevel==2 || model.newsLevel==3){
        self.star2View.hidden=NO;
        self.star2View.image=[UIImage imageNamed:@"收藏选中"];
        self.star2View.frame=self.modelFrame.star2ViewF;
    }else{
        self.star2View.hidden=YES;
    }
    if(model.newsLevel==3){
        self.star3View.hidden=NO;
        self.star3View.image=[UIImage imageNamed:@"收藏选中"];
        self.star3View.frame=self.modelFrame.star3ViewF;
    }else{
        self.star3View.hidden=YES;
    }
    
    //关注
    [self.focusBtn setTitle:@"关注" forState:UIControlStateNormal];
    self.focusBtn.titleLabel.font=KDNewsCellModelButtonFont;
    //0未关注
    if(model.attentStatus==0){
        self.focusBtn.enabled=YES;
        self.focusBtn.backgroundColor=[UIColor grayColor];
        //self.focusBtn.selected=NO;
        
    //1已关注
    }else if(model.attentStatus==1){
        self.focusBtn.backgroundColor=[UIColor blueColor];
        self.focusBtn.enabled=NO;
        //self.focusBtn.selected=YES;
    }
    self.focusBtn.frame=self.modelFrame.focusButtonF;
    
    //查看批示
    if(model.commentStatus>=1){
        self.lookBtn.hidden= NO;
        [self.lookBtn setTitle:@"查看批示" forState:UIControlStateNormal];
        self.lookBtn.titleLabel.font=KDNewsCellModelButtonFont;
        self.lookBtn.backgroundColor=[UIColor blueColor];
        self.lookBtn.frame=self.modelFrame.lookButtonF;
    }else{
        self.lookBtn.hidden= YES;
    }
    
    
    
    
    
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
#pragma mark － 事件点击区
/**  查看批示按钮被点击了 */
- (IBAction)lookBtnClick:(UIButton *)sender {
    
    
    //if (_delegate && [_delegate respondsToSelector:@selector(subscribCell:moveBtnClick:)]) {
      //  [_delegate subscribCell:self moveBtnClick:self.moveBtn];
        
   // }
    
    if (_delegate && [_delegate respondsToSelector:@selector(subscribCell:lookBtnClick:)]) {
        [_delegate subscribCell:self lookBtnClick:sender];
    }
    
}
/**  关注按钮被点击了 */
- (IBAction)focusBtnClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(subscribCell:focusBtnClick:)]) {
        [_delegate subscribCell:self focusBtnClick:sender];
    }
}


@end
